$(function () {
	$('#should').text(changeTwoDecimal(parseFloat($('#should').text())));
	
	$("#submitCar").click(function () {
		var name = $("#name").val();
		var mobile = $("#mobile").val();
		var alipayAccount = $("#alipayAccount").val();
		
		if (!name && name.length == 0) {
			$.toastWeb({message:"请完善姓名"});
			return;
		}
		
		if (!mobile && mobile.length == 0) {
			$.toastWeb({message:"请完善手机号码"});
			return;
		}else{
			var patrn=/^((\+?86)|(\(\+86\)))?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
			if (!patrn.exec(mobile)) {
				$.toastWeb({message:"手机号码格式不对"});
				return;
			}
		}
		
		if (!alipayAccount && alipayAccount.length == 0) {
			$.toastWeb({message:"请完善支付宝账号信息"});
			return;
		}
		
		var lis = $('.news-list li.list-item');
		if(lis.length == 0){
			$.toastWeb({message:'您的购物车没有产品'});
			return;
		}
		
		var stockFlag = 0;
		var products = $(".list-item");
		var goods = new Array();
		goods.push("[")
		$.each(products, function (i, n) {
			var productId = $(this).attr("productId");
			var buyCount = $(this).find(".buyCount");
			var stock = parseInt($(this).find('.stock').text());
			if($(buyCount).val() > stock){
				stockFlag++;
			}
			goods.push('{"id": ' + productId + ', "count": ' + $(buyCount).val() + '}');
			if (i < products.length - 1) {
				goods.push(',');
			}
		});
		goods.push("]");
		if(stockFlag > 0){
			$.toastWeb({message:'您有 '+stockFlag+' 件商品库存不足'});
			return;
		}
		
		$("#goods").val(goods.join(''));
		preSaleOrderForm.submit();
	});
	
	/**
	 * 数量减
	 */
	$('.c_subtract').click(function(){
		var count = parseInt($(this).parent().find('input').val());
		var result = count-1;
		var li = $(this).parents('li.list-item');
		var productId = $(li).attr('productId');
		var stock = parseInt($(li).find('.stock').text());
		if(stock >= count){
			if(count > 1){
				if(changeCount(productId,result)){
					$(this).parent().find('input').val((result));
				}
			}
		}else{
			result = stock;
			if(changeCount(productId,result)){
				$(this).parent().find('input').val((stock));
				$.toastWeb({message:'库存不足'});
			}
		}
		$('#should').text(preAmount(countTotalPrice()));
		$('#totalPrice').text(countTotalPrice());
	});
	/**
	 * 数量加
	 */
	$('.c_add').click(function(){
		var count = parseInt($(this).parent().find('input').val());
		var result = count+1;
		var li = $(this).parents('li.list-item');
		var productId = $(li).attr('productId');
		var stock = parseInt($(li).find('.stock').text());
		if(stock > count){
			if(changeCount(productId,result)){
				$(this).parent().find('input').val(result);
			}
		}else{
			$(this).parent().find('input').val((stock));
			$.toastWeb({message:'库存不足'});
		}
		$('#should').text(preAmount(countTotalPrice()));
		$('#totalPrice').text(countTotalPrice());
	});
	/**
	 * 删除按钮
	 */
	$('.cardel').click(function(){
		var li = $(this).parents('li.list-item');
		var productId = $(li).attr('productId');
		if(removeProduct(productId)){
			$(this).parents('li').remove();
			$('#should').text(preAmount(countTotalPrice()));
			$('#totalPrice').text(countTotalPrice());
		}
		var lis = $('.news-list li.list-item');
		if(lis.length == 0){
			$('#nullCars').html('<div style="padding-top: 50px; text-align: center;">您的购物车空空如也~</div>');
		}
	});
	
});

/**
 * 输入数量
 * @param _this
 * @param _stock
 */
function NumberInputKeyup(_this,_stock){
	$(_this).val(numberKeyup(_this,_stock));
	var li = $(_this).parents('li.list-item');
	var productId = $(li).attr('productId');
	var result = $(_this).val();
	if(changeCount(productId,result)){
		$('#should').text(preAmount(countTotalPrice()));
		$('#totalPrice').text(countTotalPrice());
	}
}
/**
 * 统计总金额
 * @returns
 */
function countTotalPrice(){
	var total = 0;
	$('.news-list li').each(function(){
		var number = parseInt($(this).find('.c_number input').val());
		var price = parseFloat($(this).find('.unit-price').text());
		var ji = price * number;
		total += ji;
	});
	return changeTwoDecimal(parseFloat(total));
}
/**
 * 预付款
 * @param totalPrice
 * @returns
 */
function preAmount(totalPrice){
	return changeTwoDecimal(countTotalPrice()*0.2);
}

/**
 * 更改购物车中商品数量
 * @param productId
 * @param count
 */
function changeCount(productId,count){
	var mask;
	var flag = false;
	$.ajax({
	   type: "POST",
	   url: ctx + '/web/car/changeCount',
	   async : false,
	   beforeSend: function(){
		   mask = $.maskWeb();
	   },
	   data: {openid : $('#openid').val(), productId : productId, count : count},
	   success: function(data){
		   if(!data.success){
			   $.toastWeb({message:data.message});
		   }else{
			   flag = true;
		   }
	   },
	   complete:function(){
		   $.unmaskWeb(mask);
	   }
	});
	return flag;
}

/**
 * 删除购物车中商品
 */
function removeProduct(productId){
	var mask;
	var flag = false;
	$.ajax({
	   type: "POST",
	   url: ctx + '/web/car/removeProduct',
	   async : false,
	   beforeSend: function(){
		   mask = $.maskWeb();
	   },
	   data: {openid : $('#openid').val(), productId : productId},
	   success: function(data){
		   if(data.success){
			   flag = true;
		   }
		   $.toastWeb({message:data.message});
	   },
	   complete:function(){
		   $.unmaskWeb(mask);
	   }
	});
	return flag;
}
